﻿using AutoMapper;
using PowerHub.MES.Common.NumberingCenter;
using PowerHub.MES.DTO.Shopfloor.DTO;
using PowerHub.MES.DTO.Shopfloor.QueryDTO;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities.Shopfloor;
using PowerHub.MES.Entities;
using PowerHub.MES.IRepository.IShopfloor;
using PowerHub.MES.IServices.IShopfloor;
using PowerHub.MES.Jwt;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerHub.MES.Entities.Crew;
using PowerHub.MES.DTO.Crew.QueryDto;
using PowerHub.MES.DTO.Crew.DTO;
using PowerHub.MES.IServices.ICrew;
using PowerHub.MES.IRepository.ICrew;

namespace PowerHub.MES.Services.Crew
{
    /// <summary>
    /// 班组类型接口实现层
    /// </summary>
    public class Crew_TeamTypeServices : BaseServices<Crew_TeamType>, ICrew_TeamTypeServices
    {
        private ICrew_TeamTypeRepository _iCrew_TeamTypeRepository;
        public UserServices _userServices;
        private IMapper _mapper;
        private static Random random = new Random();

        public Crew_TeamTypeServices(PowerHubMesContext powerHubMesContext, ICrew_TeamTypeRepository iCrew_TeamTypeRepository, UserServices userServices, IMapper mapper) : base(powerHubMesContext)
        {
            _iCrew_TeamTypeRepository = iCrew_TeamTypeRepository;
            _userServices = userServices;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取班组类型列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<Crew_TeamType>>> GetCrew_TeamTypePageListAsync(QueryCrew_TeamTypeDTO query)
        {
            var ss = _userServices.UserId;
            var UserName = _userServices.UserName;
            var Role = _userServices.Role;
            return await _iCrew_TeamTypeRepository.GetCrew_TeamTypePageListAsync(query);
        }

        /// <summary>
        /// 添加班组类型
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> CreateCrew_TeamTypeAsync(Crew_TeamTypeDTO query)
        {
            Crew_TeamType foundation = await GetEntityAsync(d => d.TeamTypeName.Equals(query.TeamTypeName) && d.IsDel == 0);
            if (foundation != null)
            {
                throw new Exception("你输入的班组类型名称已存在！");
            }
            //实体转换
            var Crew_TeamType = _mapper.Map<Crew_TeamType>(query);
            Crew_TeamType.Id = Guid.NewGuid().ToString();
            Crew_TeamType.TeamTypeNO = string.IsNullOrEmpty(query.TeamTypeNO) || query.TeamTypeNO.Contains(" ") ? GenerateBasicDataNO.GenerateCode(query.TeamTypeName) : query.TeamTypeNO;
            Crew_TeamType.Creator = _userServices.UserName;

            return await _iCrew_TeamTypeRepository.CreateCrew_TeamTypeAsync(Crew_TeamType);
        }


        /// <summary>
        /// 批量删除班组类型
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> DeletionCrew_TeamTypeAsync(List<string>? ids)
        {
            if (ids.Count < 1)
            {
                throw new Exception("请先选择数据！");
            }
            return await _iCrew_TeamTypeRepository.DeletionCrew_TeamTypeAsync(ids);
        }


        /// <summary>
        /// 更新班组类型
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> UpdateCrew_TeamTypeAsync(Crew_TeamTypeDTO query)
        {
            if (query == null)
            {
                throw new Exception("请先选择数据！");
            }

            //根据id获取班组类型信息
            Crew_TeamType Crew_TeamType = await GetEntityAsync(d => d.Id.Equals(query.Id));
            if (Crew_TeamType == null)
            {
                throw new Exception("更新班组类型信息发生了异常,请刷新页面后试试");
            }
            Crew_TeamType.TeamTypeName = query.TeamTypeName;
            Crew_TeamType.StartTime = query.StartTime;
            Crew_TeamType.EndTime = query.EndTime;
            Crew_TeamType.Updator = _userServices.UserName;

            return await _iCrew_TeamTypeRepository.UpdateCrew_TeamTypeAsync(Crew_TeamType);

        }
    }
}
